package cn.niit.pm.controller;


import cn.niit.pm.entity.Attendance;
import cn.niit.pm.service.impl.AttendanceServiceImpl;
import cn.niit.pm.utils.TimerUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 考勤管理页面
 * @author Yupengfei
 */
@Api
@Controller
public class AttendanceController {

    @Autowired
    private AttendanceServiceImpl service;

    /**
     * 考勤管理登录页面
     *
     * @return
     */
    @GetMapping("/attendance_management")
    public String attendanceIndex(Attendance attendance,
                                  @RequestParam(value = "pageSize", defaultValue = "0") int pageSize,
                                  @RequestParam(value = "pageNumber", defaultValue = "10") int pageNumber, Model model) {
        PageInfo info = service.select(attendance, pageSize, pageNumber);
        model.addAttribute("pageInfo", info);
        return "attendance_management/attendance_management";
    }

    @PostMapping("/attendance_management/insert")
    @ResponseBody
    public void insert(@RequestParam("employeeId") Long employeeId,
                       @RequestParam("checkTime") String checkTime,
                       @RequestParam("lateOrNot") Integer lateOrNot,
                       @RequestParam("singOutTime") String singOutTime,
                       @RequestParam("leaveEarlyOrNot") Integer leaveEarlyOrNot
    ) {
        Attendance attendance = new Attendance();
        attendance.setEmployeeId(employeeId);
        attendance.setCheckTime(new Date());
        attendance.setLateOrNot(lateOrNot);
        attendance.setSingOutTime(new Date());
        attendance.setLeaveEarlyOrNot(leaveEarlyOrNot);
        service.insert(attendance);
    }

    @PostMapping("/attendance_management/update")
    @ResponseBody
    public void save(@RequestParam("attendanceId") Long attendanceId,
                     @RequestParam("employeeId") Long employeeId,
                     @RequestParam("checkTime") String checkTime,
                     @RequestParam("lateOrNot") Integer lateOrNot,
                     @RequestParam("singOutTime") String singOutTime,
                     @RequestParam("leaveEarlyOrNot") Integer leaveEarlyOrNot
    ) {
        Attendance attendance = new Attendance();
        attendance.setAttendanceId(attendanceId);
        attendance.setEmployeeId(employeeId);
        attendance.setCheckTime(new Date());
        attendance.setLateOrNot(lateOrNot);
        attendance.setSingOutTime(new Date());
        attendance.setLeaveEarlyOrNot(leaveEarlyOrNot);
        service.update(attendance);
    }

    @GetMapping("/attendance_management/delete/{id}")
    @ResponseBody
    public void delete(@PathVariable("id") Integer id) {
        service.delete(id);
    }

    @GetMapping("/attendance_management/selectById/{id}")
    @ResponseBody
    public Attendance selectById(@PathVariable("id") Long id) {
        return service.selectById(id);
    }

}
